package com.wgj.android.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jiaai.xui.XUI;
import com.jiaai.xui.utils.XResourcesUtils;
import com.jiaai.xui.utils.http.HttpCallBack;
import com.jiaai.xui.utils.http.XHttp;
import com.jiaai.xui.view.widget.XLoadingDialog;
import com.jiaai.xui.view.widget.XToast;
import com.wgj.android.R;
import com.wgj.android.dialog.BanckCardDialog;
import com.wgj.android.entity.BankInfo;
import com.wgj.android.http.WGJAPI;
import com.wgj.android.utils.CacheUtils;
import com.wgj.android.utils.TextViweUtils;


import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/10.
 */

public class TxActivity  extends AppCompatActivity {
    private BankInfo bankInfo;
    private  BanckCardDialog banckCardDialog;
    private  double all_money;
    @BindView(R.id.bankIcon)
    ImageView bankIcon;
    @BindView(R.id.bankView)
    TextView bankView;

    @BindView(R.id.money)
    EditText evMoney;
    public  static  void  toTx(Context context){
        Intent intent=new Intent(context,TxActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.tx_activity);
        //banckCardDialog=new BanckCardDialog();
        //banckCardDialog.setBankCallback(this);
        ButterKnife.bind(this);

        XHttp.obtain().get(WGJAPI.BASEURL + "member/wallet?mId=" + CacheUtils.getMember().getM_id(), null, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                JSONObject jsonObject=JSONObject.parseObject(s);
                all_money=jsonObject.getDouble("commision");
                evMoney.setHint("当前可提现金额"+all_money);

            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XToast.error("异常");
            }
        });
    }


    @OnClick(R.id.selectBank)
    public  void onSelectBankClick(){
         //banckCardDialog.show(getSupportFragmentManager(),"BanckCardDialog");
        startActivityForResult(new Intent(this,MyBankActivity.class),1000);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1000&&resultCode==RESULT_OK){
            bankInfo = (BankInfo) data.getSerializableExtra("bankInfos");
            int iconId= XResourcesUtils.getDrableId("icon_bank_"+bankInfo.getBank_code());
            bankView.setText(bankInfo.getBank_name());
            bankIcon.setImageDrawable(XUI.getDrawable(iconId));
        }
    }

    @OnClick(R.id.all)
    public void  onAll(View view){
        evMoney.setText(String.valueOf(all_money));
    }

    @OnClick(R.id.tx)
    public  void  tx(View view){

        if(evMoney.getText().length()==0 ){
            XToast.warning("请输入你要提现的金额");
            return;
        }
        double m=Double.valueOf(evMoney.getText().toString());
        if(m==0){
            XToast.warning("提现的金额不能为0");
            return;
        }
        if(m>all_money){
            XToast.warning("你的可提现额度不足");
            return;
        }
        if(m%10>0){
            XToast.warning("请输入10元整数倍");
            return;
        }
        if(bankInfo==null){
            XToast.warning("请选择的银行卡");
            return;
        }
        XLoadingDialog.with(this).setMessage("正在处理中").show();
        JSONObject jsonObject=new JSONObject();
        jsonObject.put("bankCode",bankInfo.getBank_code());
        jsonObject.put("amount",evMoney.getText().toString());
        jsonObject.put("trueName",bankInfo.getCardholder());
        jsonObject.put("bankNum",bankInfo.getBank_no());
        jsonObject.put("mId",CacheUtils.getMember().getM_id());
        Map<String,Object> params=new HashMap<>();
        params.put("body",jsonObject.toJSONString());

        XHttp.obtain().post(WGJAPI.BASEURL + "tx/getmoney", params, new HttpCallBack<String>() {
            @Override
            public void onSuccess(String s) {
                XLoadingDialog.with(TxActivity.this).dismiss();
                JSONObject object= JSON.parseObject(s);

                if(object.getIntValue("code")==1){
                    finish();
                    XToast.success("提现成功1-2个工作日到帐",3000);
                }else {
                    XToast.error(object.getString("des"),2000);
                }


            }

            @Override
            public void onFailed(int i, String s, boolean b) {
                XLoadingDialog.with(TxActivity.this).dismiss();
                XToast.error("提现失败");
            }
        });
    }
   /* @Override
    public void onSelect(BankInfo bankInfo) {
        this.bankInfo=bankInfo;
        int iconId= XResourcesUtils.getDrableId("icon_bank_"+bankInfo.getBank_code());
        bankView.setText(bankInfo.getBank_name());
        bankIcon.setImageDrawable(XUI.getDrawable(iconId));
    }*/

}
